<?php

namespace app\admin\controller;

use app\common\BaseHelper;
use app\admin\model\Admin;

use think\Controller;
use think\Request;

class Auth extends Controller
{

    /**
     * Login
     */
    public function login()
    {
        $session_admin = session('ADMIN');
        if (!empty($session_admin->id)) {
            $this->redirect('admin/dashboard/index');
        }

        return $this->view->fetch();
    }

    /**
     * Ajax Login
     * @return \think\response\Json
     */
    public function doLogin(Request $request)
    {

        if ($request->isPost()) {
            $param = $request->param();
            $loginName = trim($param['username']);
            $password = trim($param['password']);
            if (!$loginName) {
                return show(config('status.ERROR_STATUS'), '登录名不能为空！', '登录名不能为空！');
            }
            if (!$password) {
                return show(config('status.ERROR_STATUS'), '密码不能为空！', '密码不能为空！');
            }
            $adminInfo = Admin::where(['username' => $loginName])->find();
            if ($adminInfo) {
                if ($adminInfo->type != 0 && $adminInfo->type != 1) {
                    return show(config('status.ERROR_STATUS'), '用户名不存在', '用户名不存在');
                } else {
                    if ($adminInfo->type == 0) {
                        if (BaseHelper::passwordEncrypt($password) == $adminInfo['password']) {
                            session('ADMIN', $adminInfo);
                            return show(config('status.SUCCESS_STATUS'), '登录成功', '登录成功');
                        } else {
                            return show(config('status.ERROR_STATUS'), '用户名或密码错误', '用户名或密码错误');
                        }
                    } else {
                        if ($adminInfo->is_admin == 1) {
                            if (BaseHelper::passwordEncrypt($password) == $adminInfo['password']) {
                                session('ADMIN', $adminInfo);
                                return show(config('status.SUCCESS_STATUS'), '登录成功', '登录成功');
                            } else {
                                return show(config('status.ERROR_STATUS'), '用户名或密码错误', '用户名或密码错误');
                            }
                        } else {
                            return show(config('status.ERROR_STATUS'), '您没有权限登录后台', '您没有权限登录后台');
                        }
                    }
                }
            } else {
                return show(config('status.ERROR_STATUS'), '用户名不存在', '用户名不存在');
            }
        } else {
            return show(config('status.ERROR_STATUS'), '非法请求', '非法请求');
        }
    }

    /**
     * Logout
     */
    public function logout()
    {
        session('ADMIN', null);

        $this->redirect('admin/auth/login');
    }

    /**
     * 检查用户名是否已存在
     */
    public function checkName()
    {
        $loginName = $this->request->param('loginName');
        $admin = Admin::where(['username' => $loginName, 'isDelete' => 2])->field('loginName')->find();

        if ($admin) {
            return json(['status' => 1]);
        } else {
            return json(['status' => 0]);
        }
    }
}
